//schema
//model
//entity

const mongoose = require('mongoose')

const Schema = mongoose.Schema

const TokenSchema = new Schema({
    name: String,
    token: String,
    expires_in: Number,
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt:{
            type: Date,
            default: Date.now()
        }
    }
})

TokenSchema.pre('save' ,function (next) 
{
    if(this.isNew){
        this.meta.createdAt = this.meta.updatedAt = Date.now()

    }else{
        this.meta.updatedAt = Date.now()
    }
    next()
})
TokenSchema.statics = {
    async getAccessToken () {
        const token = await this.findOne({
            name: 'access_token'
        })

        if(token && token.token){
            token.access_token = token
        }

        return token
    },
    async saveAccessToken (data) {
        let token = await this.findOne({
            name: 'access_token'
        })

        if(token){
            token.token = data.access_token
            token.expires_in = data.expires_in
        }else {
            token = new Token({
                name: 'access_token',
                token: data.access_token,
                expires_in: data.expires_in
            })
        }
        console.log("ddbdddd  save")
        await token.save()
        return data


    }
}

const Token = mongoose.model('Token',TokenSchema)

